home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2793 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Returning ref to object - is this code invalid?
  5. Date: 19 Jan 1996 19:06:20 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4doq3c$507@newsroom.hitc.com>
  8. References: <30F937D8.12D2@sierra.net>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <30F937D8.12D2@sierra.net>, snowbull@sierra.net says...
  14. >
  15. >Does the following code create a dangling reference?
  16. >
  17. >class vector
  18. >{ private: float x,y,z;
  19. >  public:  //c'tors & functions
  20. >};
  21. >
  22. >class face
  23. >{  private: vector v1, v2, v3;
  24. >   public:
  25. >        vector& get_vtx_1() const;
  26. >}
  27. >
  28. >vector& face::get_vtx_1() const  //returns reference to vector
  29. >{  return v1;
  30. >}
  31. >
  32. >void main()
  33. >{
  34. >  face ff('constructor parameters);
  35. >  vector vv = ff.get_vtx_1();
  36. >}
  37. >
  38. >I'm wondering if the return of a reference to v1 will invalidate
  39. >my program since v1 goes out of scope when get_vtx_1() returns, 
  40. >or does v1 stay in scope since ff is not deleted?
  41. >
  42. >Any input is appreciated!
  43. >
  44. >-TC
  45.  
  46. It should stay in scope until the particular face instance (ff) is 
  47. destroyed, since the vector members are assumed constructed either before 
  48. (if they are arguments to the face constructors) or while face is being 
  49. constructed...  Here, the scope is based on class existence, not file or 
  50. module scope.  It is like the new and delete operators:  until you kill 
  51. (sorry, delete) it will hang around...
  52.  
  53.  
  54. The killer is when you create an object within a METHOD (not the class 
  55. constructors) and try to return it by reference or pointer...you then 
  56. have violated scope rules and are probably peeking into the program stack 
  57. for wisdom and things to do...
  58.  
  59. If your method did this:
  60.  
  61. ...yada, yada, yada...
  62. {
  63.     vector vtemp(v1);
  64.     return &v1;
  65. }
  66.  
  67. You are in hose-city with no way out...(give your PC the 3-fingered 
  68. salute)
  69.  
  70. Hope this helps....
  71.  
  72. -- 
  73. G. Patrick Sand
  74. psand@eos.hitc.com
  75. PatSand@aol.com
  76. (301) 925-0791
  77. "Travel Light But Right..."
  78. Microsoft Network is prohibited from redistributing 
  79. this work in any form, in whole or in part.   License 
  80. to distribute this individual post is available to Microsoft
  81. for $999. Posting without permission constitutes an 
  82. agreement to these terms...gps
  83.  
  84.